package com.link2room.rodimus.facade.impl;

import com.link2room.aeriabots.dto.result.base.WxMemberInfoResDto;
import com.link2room.blaster.constant.MsgConstant;
import com.link2room.blaster.util.MessageFlatPad;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dto.param.base.BaseL2rParamDto;
import com.link2room.common.dto.result.base.MemberSessionDto;
import com.link2room.common.dto.threadlocal.ApiThreadLocal;
import com.link2room.common.enums.BaseSysExceptionEnum;
import com.link2room.common.exception.SysException;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.DateUtil;
import com.link2room.common.util.L2RAssert;
import com.link2room.common.util.StringUtil;
import com.link2room.rodimus.constant.CouponConstant;
import com.link2room.rodimus.constant.ExpressConstant;
import com.link2room.rodimus.dto.param.api.*;
import com.link2room.rodimus.dto.param.inside.CouponGiveOutInsideParamDto;
import com.link2room.rodimus.dto.result.base.*;
import com.link2room.rodimus.entity.*;
import com.link2room.rodimus.facade.IAfterPMSdepDoFacadeService;
import com.link2room.rodimus.facade.ICouponFacadeService;
import com.link2room.rodimus.feign.aeriabots.WxMemberInfoFeignService;
import com.link2room.rodimus.feign.soundwave.PmsMemberFeignService;
import com.link2room.rodimus.service.ICouponService;
import com.link2room.rodimus.service.IExpressService;
import com.link2room.rodimus.service.IMemberService;
import com.link2room.soundwave.dto.param.inside.MemberCoupenCodeCollarCoupenInsideParamDto;
import com.link2room.soundwave.dto.param.inside.PmsReservationInsideParamDto;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.link2room.enums.exception.RodimusSysExceptionEnum.COUPON_ERROR;
import static com.link2room.enums.exception.RodimusSysExceptionEnum.DRAW_ERROR;
import static com.link2room.rodimus.constant.CouponConstant.PRESENT_RESET_DELAY;

/**
 * Created by LN on 2017/7/24.11:33
 */
@Component("couponFacadeService")
public class CouponFacadeServiceImpl implements ICouponFacadeService, IAfterPMSdepDoFacadeService {
    private static Logger logger = LoggerFactory.getLogger(CouponFacadeServiceImpl.class);

    @Resource
    private ICommonService commonService;
    @Resource
    private ICouponService couponService;
    @Resource
    private IExpressService expressService;
    @Resource
    private IMemberService memberService;

    @Autowired
    private WxMemberInfoFeignService wxMemberInfoFeignService;

    @Resource
    private MessageFlatPad messageFlatPad;
    
    @Autowired
    private PmsMemberFeignService pmsMemberFeignService;

    @Override
    public List<CouponConvergeDto> getCurrentCouponList(CouponParamDto paramDto) throws Exception {
        MemberSessionDto memberSession = ApiThreadLocal.getMemberSessionThreadLocal();
        String openid = memberSession.getOpenid();
        L2RAssert.stringBlank(openid, "session error:[openid] is null.");
        QueryResult<CouponConvergeDto> qres = commonService.findByNative(
                findUserCoupon(openid),
                null,
                Collections.EMPTY_LIST,
                Collections.EMPTY_LIST,
                CouponConvergeDto.class,
                null,
                null);
        if (CollectionUtils.isNotEmpty(qres.getResultlist())) {
            qres.getResultlist().forEach(dto -> {
                if (dto == null || StringUtil.isEmpty(dto.getCouponCode())) {
                    return;
                } else {
                    String couponCode = dto.getCouponCode();
                    CouponGoodEntity find = new CouponGoodEntity();
                    find.setCouponCode(couponCode);
                    try {
                        CouponGoodEntity _qres = commonService.findOneEQ(CouponGoodEntity.class, find);
                        if (_qres != null) {
                            dto.setGoodCode(_qres.getGoodCode());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        return qres.getResultlist();
    }

    @Override
    public CouponDetailDto getCouponDetail(CouponParamDto paramDto) throws Exception {
        String couponNo = paramDto.getCouponNo();
        CouponDetailEntity find = new CouponDetailEntity();
        find.setLhotelGroupCode(paramDto.getLhotelGroupCode());
        find.setCouponNo(couponNo);
        CouponDetailEntity qres = commonService.findOneEQ(CouponDetailEntity.class, find);
        if (qres == null) {
            return null;
        } else {
            return ClassConverUtil.copyProperties(qres, CouponDetailDto.class);
        }
    }

    @Override
    public CouponPresentationDto presentCoupon(CouponPresentParamDto paramDto) throws Exception {
        Integer numPerPart = paramDto.getNumPerPart();
        L2RAssert.isTrue(numPerPart == null || numPerPart <= 0, new SysException(COUPON_ERROR, "[numPerPart] 不合法."));
        Integer partsNum = paramDto.getPartsNum();
        L2RAssert.isTrue(partsNum == null || partsNum <= 0, new SysException(COUPON_ERROR, "[partsNum] 不合法."));
        String couponCode = paramDto.getCouponCode();
        L2RAssert.stringBlank(couponCode, "[couponCode] is null.");
        Integer total = numPerPart * partsNum;
        MemberSessionDto memberSession = ApiThreadLocal.getMemberSessionThreadLocal();
        String openid = memberSession.getOpenid();
        CouponDetailEntity find = new CouponDetailEntity();
        find.setOpenid(openid);
        find.setSta(CouponConstant.COUPON_STA.VALID);
        find.setCouponCode(couponCode);
        find.setLhotelGroupCode(paramDto.getLhotelGroupCode());
        QueryResult<CouponDetailEntity> qCouponDetail = commonService.findAllEQ(CouponDetailEntity.class, find, 1, total);
        L2RAssert.isTrue(
                qCouponDetail == null || qCouponDetail.getResultlist().size() < total,
                new SysException(COUPON_ERROR, "当前有" + qCouponDetail.getResultlist().size() + "张券可分享,不够本次分享" + total + "张"));
        CouponGoodEntity findGoodMap = new CouponGoodEntity();
        findGoodMap.setCouponCode(couponCode);
        CouponGoodEntity couponGoodEntity = commonService.findOneEQ(CouponGoodEntity.class, findGoodMap);
        String goodCode = null;
        if (couponGoodEntity != null) {
            goodCode = couponGoodEntity.getGoodCode();
        }
        //生成couponPresentBase
        CouponPresentBaseEntity presentBaseEentity = initPresentBaseEntity(paramDto, openid, goodCode);
        //生成明细
        List<CouponPresentDetailEntity> cpdList = new ArrayList<>();
        qCouponDetail.getResultlist().stream()
                .forEach(entity -> {
                    CouponPresentDetailEntity cpd = new CouponPresentDetailEntity();
                    ClassConverUtil.copyProperties(presentBaseEentity, cpd);
                    cpd.setFromUser(openid);
                    cpd.setCouponCode(entity.getCouponCode());
                    cpd.setIsPresend(DefaultValue.F);
                    cpd.setCouponNo(entity.getCouponNo());
                    cpdList.add(cpd);
                    entity.setSta(CouponConstant.COUPON_STA.GIVING);
                    entity.setGiveEndDate(presentBaseEentity.getGiveEndDate());
                });
        couponService.savePresent(presentBaseEentity, cpdList, qCouponDetail.getResultlist());
        String sign = couponService.couponSign(openid, presentBaseEentity.getPresentNo());
        CouponPresentationDto res = new CouponPresentationDto();
        res.setCouponPresentNo(presentBaseEentity.getPresentNo());
        res.setPresentSign(sign);
        return res;
    }

    @Override
    public void drawCoupon(CouponDrawParamDto paramDto) throws Exception {
        String couponPresentNo = paramDto.getCouponPresentNo();
        L2RAssert.stringBlank(couponPresentNo, "[couponPresentNo] is null.");
        String presentSign = paramDto.getPresentSign();
        L2RAssert.stringBlank(presentSign, "[presentSign] is null.");
        MemberSessionDto memberSession = ApiThreadLocal.getMemberSessionThreadLocal();
        String openid = memberSession.getOpenid();
        couponService.checkCouponPresent(couponPresentNo, presentSign, openid);
        couponService.doDraw(couponPresentNo, openid, paramDto.getRemark());
    }

    @Override
    public List<CouponPresentDto> currentUserPresentList(BaseL2rParamDto baseL2rParamDto) throws Exception {
        MemberSessionDto memberSession = ApiThreadLocal.getMemberSessionThreadLocal();
        String openid = memberSession.getOpenid();


        HashMap<String, String> orderBy = new HashMap<>();
        orderBy.put(CouponPresentBaseEntity.BaseEntity_.id.toString(), CriteriaUtil.DESC.toString());

        CouponPresentBaseEntity find = new CouponPresentBaseEntity();
        find.setOpenid(openid);
        QueryResult<CouponPresentBaseEntity> qres = commonService.findAllEQ(
                CouponPresentBaseEntity.class, find, baseL2rParamDto.getCurrentPage(),
                baseL2rParamDto.getPageSize(), orderBy, null);
        if (qres == null && CollectionUtils.isEmpty(qres.getResultlist())) {
            return Collections.emptyList();
        } else {
            return qres.getResultlist().stream()
                    .map(couponPresentBaseEntity -> ClassConverUtil.copyProperties(couponPresentBaseEntity, CouponPresentDto.class))
                    .collect(Collectors.toList());
        }
    }

    @Override
    public CouponPresentationDto rePresentCoupon(CouponRePresentParamDto paramDto) throws Exception {
        String couponPresentNo = paramDto.getCouponPresentNo();
        L2RAssert.stringBlank(couponPresentNo, "[couponPresentNo] is null.");
        MemberSessionDto memberSession = ApiThreadLocal.getMemberSessionThreadLocal();
        String openid = memberSession.getOpenid();
        CouponPresentBaseEntity find = new CouponPresentBaseEntity();
        find.setOpenid(openid);
        find.setPresentNo(couponPresentNo);
        CouponPresentBaseEntity qres = commonService.findOneEQ(CouponPresentBaseEntity.class, find);
        L2RAssert.isTrue(qres == null, new SysException(COUPON_ERROR, "分享不存在"));
        L2RAssert.isTrue(CouponConstant.PRESEND_STA.STA_GIVEN.equals(qres.getSta()), new SysException(COUPON_ERROR, "已经全部分享完"));
//        commonService.updateById(qres);
        String sign = couponService.couponSign(openid, qres.getPresentNo());
        CouponPresentationDto res = new CouponPresentationDto();
        res.setCouponPresentNo(qres.getPresentNo());
        res.setPresentSign(sign);
        return res;
    }

    @Override
    public CouponPresentDto listCouponReceived(CouponReceiveParamDto paramDto) throws Exception {
        String couponPresentNo = paramDto.getCouponPresentNo();
        L2RAssert.stringBlank(couponPresentNo, "[couponPresentNo] is null.");
        MemberSessionDto memberSession = ApiThreadLocal.getMemberSessionThreadLocal();
        String openid = memberSession.getOpenid();
        String hasDraw = couponService.hasPresentDrew(openid, couponPresentNo) ? DefaultValue.T : DefaultValue.F;

        CouponPresentBaseEntity find = new CouponPresentBaseEntity();
        find.setPresentNo(couponPresentNo);
        CouponPresentBaseEntity pBaseEntity = commonService.findOneEQ(CouponPresentBaseEntity.class, find);
        CouponPresentDto res = ClassConverUtil.copyProperties(pBaseEntity, CouponPresentDto.class);
        res.setHasDrew(hasDraw);
        res.setPresentNo(couponPresentNo);

        QueryResult<CouponPresentDetailEntity> qres = _listCouponReceived(couponPresentNo, paramDto.getCurrentPage(), paramDto.getPageSize());
        if (qres == null || CollectionUtils.isEmpty(qres.getResultlist())) {
            res.setPresentDetails(Collections.EMPTY_LIST);
            res.setTotalrecord(0);
            res.setCurrentPage(1);
            return res;
        } else {
            List<CouponPresentDetailDto> details = qres.getResultlist().stream()
                    .map(entity -> ClassConverUtil.copyProperties(entity, CouponPresentDetailDto.class))
                    .collect(Collectors.toList());
            details.forEach(detailDto -> {
                String userOpenid = detailDto.getToUser();
                try {//查询微信姓名等信息

                    WxMemberInfoResDto memberInfo = wxMemberInfoFeignService.getMemberInfos(paramDto.getLhotelGroupCode(),openid).getResultInfo();
                    detailDto.setNickName(memberInfo.getNickname());
                    detailDto.setHeadimgurl(memberInfo.getHeadimgurl());
                } catch (Exception e) {
                    throw new SysException(COUPON_ERROR, "aeriabotsAPIService" + e.getMessage());
                }
            });
            res.setPresentDetails(details);
            res.setTotalrecord(qres.getTotalrecord());
            res.setCurrentPage(qres.getCurrentPage());
            return res;
        }

    }

    @Override
    @Transactional
    public CouponConsumeDto couponConsumWithExpress(CouponConsumeParamDto paramDto) throws Exception {
        String address = paramDto.getAddress();
        L2RAssert.stringBlank(address, "[address] is null.");
        String mobile = paramDto.getMobile();
        L2RAssert.stringBlank(mobile, "[mobile] is null.");
        String receiveName = paramDto.getReceiverName();
        L2RAssert.stringBlank(receiveName, "[receiveName] is null.");
        //消费电子券
        CouponConsumeDto couponConsumeDto = couponService.couponConsum(paramDto);
        ;

        MemberSessionDto memberSession = ApiThreadLocal.getMemberSessionThreadLocal();
        String openid = memberSession.getOpenid();

        String sourceNo = couponConsumeDto.getConsumeNo();
        String type = ExpressConstant.SOURCE_TYPE.COUPON;
        //收货地址
        ExpressBaseEntity entity = expressService.initExpress();
        entity.setLhotelGroupCode(paramDto.getLhotelGroupCode());
        entity.setAddress(address);
        entity.setReceiverName(receiveName);
        entity.setSourceNo(sourceNo);
        entity.setSourceType(type);
        entity.setGoodAmount(couponConsumeDto.getAmount());
        entity.setGoodCode(couponConsumeDto.getGoodCode());
        entity.setGoodName(couponConsumeDto.getGoodName());
        entity.setOpenid(openid);
        entity.setMemberNo(memberSession.getMemberNo());
        entity.setMobile(mobile);
        entity.setRemark(paramDto.getRemark());
        commonService.save(entity);
        return couponConsumeDto;
    }

    @Override
    public CouponConsumeDto couponConsum(CouponConsumeParamDto paramDto) throws Exception {
        return couponService.couponConsum(paramDto);
    }

    @Override
    public QueryResult currentUserCouponConsumList(CouponConsumeParamDto paramDto) throws Exception {
        String couponCode = paramDto.getCouponCode();
        MemberSessionDto memberSession = ApiThreadLocal.getMemberSessionThreadLocal();
        String openid = memberSession.getOpenid();
        L2RAssert.stringBlank(openid, "session error , openid is null.");

        HashMap<String, String> orderBy = new HashMap<>();
        orderBy.put(CouponConsumeBaseEntity.BaseEntity_.id.toString(), CriteriaUtil.DESC);

        CouponConsumeBaseEntity find = new CouponConsumeBaseEntity();
        find.setCouponCode(couponCode);
        find.setOpenid(openid);
        find.setSta(paramDto.getConsumeSta());
        QueryResult<CouponConsumeBaseEntity> qres = commonService.findAllEQ(CouponConsumeBaseEntity.class, find, paramDto.getCurrentPage(), paramDto.getPageSize(), orderBy, null);

        QueryResult dtos = new QueryResult();
        dtos.setTotalrecord(qres.getTotalrecord());
        dtos.setCurrentPage(qres.getCurrentPage());
        dtos.setResultlist(qres.getResultlist().stream().map(entity -> ClassConverUtil.copyProperties(entity, CouponConsumeBaseDto.class)).collect(Collectors.toList()));
        return dtos;
    }

    @Override
    public void presentCouponReset(CouponPresentResetParamDto paramDto) throws Exception {
        String presentNo = paramDto.getCouponPresentNo();
        L2RAssert.stringBlank(presentNo, "[presentNo] is null.");
        CouponPresentBaseEntity find = new CouponPresentBaseEntity();
        find.setPresentNo(presentNo);
        find.setSta(CouponConstant.PRESEND_STA.STA_GIVING);
        CouponPresentBaseEntity presentBase = commonService.findOneEQ(CouponPresentBaseEntity.class, find);
        if (presentBase == null) {
            return;
        }
        couponService.resetOresentCoupon(presentNo);
    }

    @Override
    public void putOutCoupon(CouponGiveOutInsideParamDto paramDto) throws Exception {
        String openid = paramDto.getOpenid();
        L2RAssert.stringBlank(openid, "[openid] is null.");
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGroupCode] is null.");
        String couponCode = paramDto.getCouponCode();
        L2RAssert.stringBlank(couponCode, "[couponCode] is null.");
        Integer num = paramDto.getNum();
        L2RAssert.numberNull(num, "[num] is null");
        String saleType = paramDto.getSaleType();
        L2RAssert.stringBlank(saleType, "[saleType] is null.");
        String orderNo = paramDto.getOrderNo();
        String memberNo = paramDto.getMemberNo();
        if (StringUtil.isBlank(memberNo)) {
            MemberBase memberBase = memberService.selectMemberByOpenid(lhotelGroupCode, openid);
            memberNo = memberBase.getMemberNo();
        }
        List<CouponDetailEntity> couponDetail = couponService.createNewCouponDetail(orderNo, lhotelGroupCode, couponCode, openid, num, saleType, memberNo);
        commonService.saveList(couponDetail);
    }


    private QueryResult<CouponPresentDetailEntity> _listCouponReceived(String couponPresentNo, Integer currentPage, Integer pageSize) throws Exception {
        HashMap<String, String> orderBy = new HashMap<>();
        orderBy.put(CouponPresentBaseEntity.BaseEntity_.id.toString(), CriteriaUtil.DESC);
        String[] groupBy = {CouponPresentDetailEntity.CouponPresentDetailEntity_.toUser.toString()};
        CouponPresentDetailEntity findDetail = new CouponPresentDetailEntity();
        findDetail.setPresentNo(couponPresentNo);
        findDetail.setIsPresend(DefaultValue.T);
        return commonService.findAllEQ(
                CouponPresentDetailEntity.class,
                findDetail,
                currentPage,
                pageSize,
                orderBy,
                groupBy);
    }

    private CouponPresentBaseEntity initPresentBaseEntity(CouponPresentParamDto paramDto, String openid, String goodCode) throws Exception {
        String couponPresendNo = couponService.generateCouponPresentNo();
        CouponPresentBaseEntity newEntity = new CouponPresentBaseEntity();
        newEntity.setLhotelGroupCode(paramDto.getLhotelGroupCode());
        newEntity.setGiveEndDate(DateUtil.addHours(new Date(), PRESENT_RESET_DELAY));
        newEntity.setPresentNo(couponPresendNo);
        newEntity.setRemark(paramDto.getRemark());
        newEntity.setReceivedNum(0);
        newEntity.setOpenid(paramDto.getOpenid());
        newEntity.setSta(CouponConstant.PRESEND_STA.STA_GIVING);
        newEntity.setGoodCode(goodCode);
        newEntity.setPartsNum(paramDto.getPartsNum());
        newEntity.setNumPerPart(paramDto.getNumPerPart());
        newEntity.setOpenid(openid);
        return newEntity;
    }

    private String findUserCoupon(String openid) {
        String sql =
                "SELECT \n" +
                        "  base.code couponCode,\n" +
                        "  base.coupon_name couponName,\n" +
                        "  base.descript descript,\n" +
                        "  detail.allCount allCount,\n" +
                        "  detail.gaveCount gaveCount,\n" +
                        "  detail.validCount validCount,\n" +
                        "  detail.usedCount usedCount,\n" +
                        "  detail.givingCount givingCount \n" +
                        "FROM\n" +
                        "  `coupon_base` base \n" +
                        "  INNER JOIN \n" +
                        "    (SELECT \n" +
                        "      a.coupon_code CODE,\n" +
                        "      COUNT(*) allCount,\n" +
                        "      COUNT(IF(a.sta = 'GAVE', TRUE, NULL)) gaveCount,\n" +
                        "      COUNT(IF(a.sta = 'VALID', TRUE, NULL)) validCount,\n" +
                        "      COUNT(IF(a.sta = 'USED', TRUE, NULL)) usedCount,\n" +
                        "      COUNT(IF(a.sta = 'GIVING', TRUE, NULL)) givingCount \n" +
                        "    FROM\n" +
                        "      (SELECT \n" +
                        "        coupon_code,\n" +
                        "        sta \n" +
                        "      FROM\n" +
                        "        `coupon_detail` \n" +
                        "      WHERE openid = '" + openid + "'" +
                        " AND is_halt = 'F'" + ") a \n" +
                        "  " +
                        "    GROUP BY a.coupon_code) detail \n" +
                        "    ON detail.code = base.code ";
        return sql;
    }

    @Override
    public void afterPMSDepDo(PmsReservationInsideParamDto pmsReservationInsideParamDto) throws Exception {
        String lhotelGroupCode = pmsReservationInsideParamDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode, "[lhotelGorupCode] is null.");
        CouponScheduleEntity find = new CouponScheduleEntity();
        find.setIsHalt(DefaultValue.F);
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setOccasion(CouponConstant.COUPON_SCHEDULE_OCCASION.AFTER_DEP);
        QueryResult<CouponScheduleEntity> qres = commonService.findAllEQ(CouponScheduleEntity.class, find);
        String orderNo =  pmsReservationInsideParamDto.getCrsNo();
        if (qres == null || CollectionUtils.isEmpty(qres.getResultlist())) {
            logger.warn("No CouponScheduleEntity for " + lhotelGroupCode + " [occasion] " + CouponConstant.COUPON_SCHEDULE_OCCASION.AFTER_DEP);
        } else {
            qres.getResultlist().stream()
//                    .filter(couponScheduleEntity -> !hasPutCoupon(lhotelGroupCode, orderNo, couponScheduleEntity.getCouponCode()))
                    .forEach(
                            couponScheduleEntity -> {
                                CouponGiveOutInsideParamDto paramDto = new CouponGiveOutInsideParamDto();
                                paramDto.setCouponCode(couponScheduleEntity.getCouponCode());
                                paramDto.setNum(couponScheduleEntity.getNum());
                                paramDto.setMemberNo(pmsReservationInsideParamDto.getMemberNo());
                                paramDto.setOrderNo(orderNo);
                                paramDto.setSaleType(CouponConstant.SALE_TYPE.DEPARTURE);
                                paramDto.setOpenid(pmsReservationInsideParamDto.getOpenid());
                                try {
                                    this.putOutCoupon(paramDto);
                                    //微信会员已经注册为我们的会员,则进行通知,否则不通知
                                    MemberBase member = memberService.selectMemberByOpenid(paramDto.getLhotelGroupCode(), pmsReservationInsideParamDto.getOpenid());
                                    if(member != null && DefaultValue.T.equals(member.getIsbindPms())){
                                    Map news = new HashMap();
                                    news.put("openid", pmsReservationInsideParamDto.getOpenid());
                                        messageFlatPad.send(pmsReservationInsideParamDto.getLhotelGroupCode(), MsgConstant.CHANNEL.WECHA_CUSTOM_SEND, MsgConstant.TEMPLET_CODE.DRAW_COUPON_GIVEOUT, news);
                                    }
                                } catch (Exception e) {
                                    throw new SysException(BaseSysExceptionEnum.BASE_FUCATION_OPTION, e);
                                }
                            }
                    );
        }

    }

    /**
     * 是否已经发放过电子券
     *
     * @param orderNo
     * @param couponCode
     * @return
     */
    private boolean hasPutCoupon(String lhotelGroupCode, String orderNo, String couponCode) {
        CouponDetailEntity find = new CouponDetailEntity();
        find.setOrderNo(orderNo);
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setCouponCode(couponCode);
        long countAllEQ = 0;
        try {
            countAllEQ = commonService.getCountAllEQ(CouponDetailEntity.class, find, null);
        } catch (Exception e) {
            throw new SysException(BaseSysExceptionEnum.BASE_FUCATION_OPTION, e);
        }
        return countAllEQ > 0;
    }

	@Override
	public void coupenCodeCollarCoupen(CoupenCodeCollarCoupenParamDto paramDto) throws Exception {
		MemberSessionDto memberSessionDto = ApiThreadLocal.getMemberSessionThreadLocal();
		L2RAssert.isTrue(!DefaultValue.T.equals(memberSessionDto.getIsMember()), new SysException(COUPON_ERROR, "非微信会员不能兑换.",null));
        L2RAssert.isTrue(!DefaultValue.T.equals(memberSessionDto.getIsbindPms()), new SysException(COUPON_ERROR, "未绑定pms不能兑换.",null));
		MemberCoupenCodeCollarCoupenInsideParamDto coupenInsideParamDto = new MemberCoupenCodeCollarCoupenInsideParamDto();
		coupenInsideParamDto.setCouponsCode(paramDto.getCouponCode());
		coupenInsideParamDto.setCouponsCodeType(paramDto.getCouponCodeType());
		coupenInsideParamDto.setL2rMemberNo(memberSessionDto.getMemberNo());
		pmsMemberFeignService.coupenCodeCollarCoupen(coupenInsideParamDto);
	}


}
